/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is NRL Order It.
 *
 * The Initial Developer of the Original Code is Dr. Christopher R. Field.
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * 		Christopher R. Field <christopher.field@nrl.navy.mil, cfield2@gmail.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
Components.utils.import("resource://NRLOrderIt/common.js");
Components.utils.import("resource://NRLOrderIt/log4moz.js");

/**
 * The <code>Item.Panel</code> module.
 */
mil.navy.nrl.NRLOrderIt.PurchaseOrder.Item.Panel = (function () {
	
	/*
	 * Private variables.
	 */
	
	var _logger = Log4Moz.repository.getLogger("NRLOrderIt.PurchaseOrder.Item.Panel");
	var _observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
	var _NRLOrderIt = mil.navy.nrl.NRLOrderIt;
	var _purchaseOrder = null;
	var _form = null;
	var _panel = null;
	
	/*
	 * Private methods.
	 */
	
	var _edit = function () {
		_logger.trace("_edit opened");
		
		let selectedTreeItem = _NRLOrderIt.PurchaseOrder.Item.Tree.selectedValue;
		if ( selectedTreeItem !== null ) {
			_NRLOrderIt.PurchaseOrder.Item.Form.load(selectedTreeItem);
		}
		else {
			_logger.warn("selectedTreeItem is null");
		}
		
		_logger.trace("_edit closed");
	};
	
	var _remove = function () {
		_logger.trace("_remove opened");
		
		let index = _NRLOrderIt.PurchaseOrder.Item.Tree.selectedIndex;
		_purchaseOrder.items.splice(index, 1);
		_NRLOrderIt.PurchaseOrder.Item.Tree.load(_purchaseOrder.items);
		
		_logger.trace("_remove closed");
	};
	
	var _removeAll = function () {
		_logger.trace("_removeAll opened");
		
		_purchaseOrder.items = [];
		_NRLOrderIt.PurchaseOrder.Item.Tree.load(_purchaseOrder.items);
		
		_logger.trace("_removeAll closed");
	};
	
	return {
		
		/*
		 * IDs.
		 */
		
		get ID() {
			return _NRLOrderIt.PurchaseOrder.Item.NAMESPACE + _NRLOrderIt.NAMESPACE_SEPARATOR + 'Panel';
		},
		
		/*
		 * Events.
		 */
		
		get EVENT_ID() {
			return this.ID + _NRLOrderIt.NAMESPACE_SEPARATOR + 'Event';
		},
		
		get LOAD_EVENT() {
			return this.EVENT_ID + _NRLOrderIt.NAMESPACE_SEPARATOR + 'Load';
		},
		
		/*
		 * Public methods.
		 */
		
		init : function (aStorage) {
			_logger.trace("init opened");
			
			if ( aStorage === null ) {
				_logger.warn("aStorage is null");
			}
			
			_panel = document.getElementById(this.ID);
			_form = document.getElementById(_NRLOrderIt.PurchaseOrder.Item.Form.ID);
			
			_NRLOrderIt.PurchaseOrder.Item.Tree.init();
			_NRLOrderIt.PurchaseOrder.Item.Form.init(aStorage);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Form.ADD_EVENT);
					
					let formItem = _NRLOrderIt.PurchaseOrder.Item.Form.item;
					
					if ( formItem === null ) {
						_logger.warn("formItem is null");	
					}
					else {
						let callback = {
							completed : function (results) {
								_NRLOrderIt.Item.Tree.load(_purchaseOrder);
							}	
						};
								
						if ( formItem.id === null ) {
							_storage.add(formItem, callback);
						}
						else {
							_storage.edit(formItem, callback);
						}
						
						_NRLOrderIt.PurchaseOrder.Item.Form.clear();
					}
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Form.ADD_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Form.EDIT_EVENT);
					
					_NRLOrderIt.PurchaseOrder.Item.Tree.load(_purchaseOrder);
					_NRLOrderIt.PurchaseOrder.Item.Form.clear();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Form.EDIT_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Tree.EDIT_EVENT);
					
					_edit();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Tree.EDIT_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Toolbar.EDIT_EVENT);
					
					_edit();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Toolbar.EDIT_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Toolbar.REMOVE_EVENT);
					
					_remove();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Toolbar.REMOVE_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Toolbar.REMOVE_ALL_EVENT);
					
					_removeAll();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Toolbar.REMOVE_ALL_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Menu.EDIT_EVENT);
					
					_edit();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Menu.EDIT_EVENT, null);
			
			_observerService.addObserver({
				observe : function (aSubject, aTopic, aData) {
					_logger.trace("observe opened");
					_logger.debug(_NRLOrderIt.PurchaseOrder.Item.Menu.REMOVE_EVENT);
					
					_remove();
					
					_logger.trace("observe closed");
				}
			}, _NRLOrderIt.PurchaseOrder.Item.Menu.REMOVE_EVENT, null);
			
			_logger.trace("init closed");
		},
		
		load : function (aPurchaseOrder) {
			_logger.trace("load opened");
			
			if ( aPurchaseOrder === null ) {
				_logger.warn("aPurchaseOrder is null");
			}
			else {
				_purchaseOrder = aPurchaseOrder;
				
				_NRLOrderIt.PurchaseOrder.Item.Tree.load(_purchaseOrder);	
			}
			
			_observerService.notifyObservers(this, this.LOAD_EVENT, null);
			_logger.trace("load closed");
		}
	}
})();